package com.kzd.gtgame.main.access

import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.jetpack.vm.AccessViewModel
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_access_phone_bind.*
import java.util.concurrent.TimeUnit

class AccessPhoneBindFragment : OwnerViewModeFragment<AccessViewModel>() {

    companion object {
        fun newInstance(): AccessPhoneBindFragment {
            return AccessPhoneBindFragment()
        }
    }

    override fun viewModel(): AccessViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_phone_bind

    override fun initView() {
        etAccessPhoneBindPhone.addTextChangedListener {
            tvAccessPhoneBindSubmit.isEnabled = !it.isNullOrEmpty() &&
                    !etAccessPhoneBindCode.text.isNullOrEmpty()
            tvAccessPhoneBindGetCode.isEnabled = !it.isNullOrEmpty() && !isCodeDownTime
        }
        etAccessPhoneBindCode.addTextChangedListener {
            tvAccessPhoneBindSubmit.isEnabled = !it.isNullOrEmpty() &&
                    !etAccessPhoneBindPhone.text.isNullOrEmpty()
        }

        tvAccessPhoneBindGetCode.setOnClickAntiShakeListener {
            viewModel.accessGetCode(etAccessPhoneBindPhone.text.toString(), type = 0)
        }

        tvAccessPhoneBindSubmit.setOnClickAntiShakeListener {
            viewModel.accessBindPhone(
                etAccessPhoneBindPhone.text.toString(),
                etAccessPhoneBindCode.text.toString()
            )
        }

    }

    override fun initViewMode() {
        viewModel.accessGetCodeLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            startCodeDownTime()
        })
    }


    private var isCodeDownTime = false
    private var disposable: Disposable? = null

    /**
     * 获取验证码倒计时
     * */
    fun startCodeDownTime() {
        disposable = Flowable.intervalRange((1), (60), (0), (1), TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext {
                tvAccessPhoneBindGetCode.text = "剩${60 - it}秒"
            }
            .doOnSubscribe {
                isCodeDownTime = true
                tvAccessPhoneBindGetCode.isEnabled = false
            }
            .doOnComplete {
                isCodeDownTime = false
                tvAccessPhoneBindGetCode.text = "发送验证码"
                tvAccessPhoneBindGetCode.isEnabled =
                    !etAccessPhoneBindPhone.text.isNullOrEmpty()
            }
            .subscribe()
        viewModel.dispose.add(disposable)
    }

}